package com.winterchen.controller.websocket;

import com.winterchen.model.jsonResult.JsonResult;
import com.winterchen.websocket.WebSocketServer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @className CheckCenterController
 * @Description 关于webSocket的Controller
 * @Author Administrator
 * @Date 2018/9/13 0013 10:36
 * @Version 1.0
 **/
@Controller
@RequestMapping("/checkcenter")
public class CheckCenterController {

    //页面请求
    @GetMapping("/socket/{cid}")
    public String socket(HttpServletRequest request, @PathVariable String cid) {
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
        ModelAndView mav=new ModelAndView("/socket");
        mav.addObject("cid", cid);
        request.setAttribute("basePath",basePath);
        return "socket/socket";
    }


    //推送数据接口
    @ResponseBody
    @RequestMapping(value = {"/socket/push/msg/{sid}","/socket/push/msg"})
    public JsonResult pushToWeb(@PathVariable(required = false) String sid, String message) {
        try {
            WebSocketServer.sendInfo(message,sid);
        } catch (IOException e) {
            e.printStackTrace();
            return JsonResult.err(sid+"#"+e.getMessage());
        }
        return JsonResult.ok(sid);
    }


}
